package com.stox.ui.javafx.charting.draw.shape;

import javafx.geometry.Bounds;
import javafx.geometry.VPos;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;

import com.stox.dto.preference.PreferenceFactory;
import com.stox.ui.javafx.charting.model.ChartingPreference;
import com.stox.ui.javafx.charting.widget.ResizableCanvas;
import com.stox.util.Constant;

public class VerticalLine extends AbstractLine {

	public VerticalLine(ResizableCanvas canvas) {
		super(canvas);
	}
	
	@Override
	public void paint() {
		double x = getPoint().getX();
		if(x <= 0) return;
		Bounds bounds = getCanvas().getLayoutBounds();
		GraphicsContext gc = getCanvas().getGraphicsContext2D();
		// TODO : Get color from settings
		gc.setLineWidth(0.5);
		gc.setStroke(Color.LIGHTBLUE);
		double minY = bounds.getMinY();
		double maxY = bounds.getMaxY();
		gc.strokeLine(x, minY, x, maxY);
		
		String strText = Constant.dateFormat.format(getPoint().getDate());
		Text text = new Text(strText);
		double indicatorArrowHeight = 6;
		double indicatorPadding = 2;
		double indicatorHeight = text.getLayoutBounds().getHeight() + indicatorPadding*2;
		double indicatorWidth = text.getLayoutBounds().getWidth() + indicatorPadding*2;
		
		double[] xCoordinates = new double[]{x - indicatorWidth/2, x - indicatorWidth/2, x, x + indicatorWidth/2, x + indicatorWidth/2};
		double[] yCoordinates = new double[]{maxY, maxY - indicatorHeight, maxY - indicatorHeight - indicatorArrowHeight, maxY - indicatorHeight, maxY}; 
		gc.setFill(Color.LIGHTBLUE);
		gc.fillPolygon(xCoordinates,yCoordinates, xCoordinates.length);
		Color backgroundColor = PreferenceFactory.get(ChartingPreference.class).getBackgroundColor();
		gc.setStroke(backgroundColor);
		gc.strokePolyline(xCoordinates, yCoordinates, xCoordinates.length);
		gc.setFill(backgroundColor);
		gc.setTextAlign(TextAlignment.CENTER);
        gc.setTextBaseline(VPos.CENTER);
		gc.fillText(strText,x, maxY - indicatorHeight/2);
	}

}
